#include "lib/system.h"
#include <stdio.h>
#include <string.h>

#include <math/math.h>



union FI{
	int i;
	float f;
};

int test(){
	volatile float a;
	volatile float b;
	volatile float c;
	volatile float d;
	volatile float e;
	a = -1.1f;
	b = -0.2f;
	c = 0;
	d = 0.2f;
	e = 1.2f;
	int r[32];
	r[0] = a > a;
	r[1] = a > b;
	r[2] = a > c;
	r[3] = a > d;
	r[4] = a > e;
	r[5] = a >= a;
	r[6] = a >= b;
	r[7] = a >= c;
	r[8] = a >= d;
	r[9] = a >= e;
	r[10] = a < a;
	r[11] = a < b;
	r[12] = a < c;
	r[13] = a < d;
	r[14] = a < e;
	r[15] = a <= a;
	r[16] = a <= b;
	r[17] = a <= c;
	r[18] = a <= d;
	r[19] = a <= e;
	r[20] = a == a;
	r[21] = a == b;
	r[22] = a == c;
	r[23] = a == d;
	r[24] = a == e;
	r[25] = a != a;
	r[26] = a != b;
	r[27] = a != c;
	r[28] = a != d;
	r[29] = a != e;
	r[30] = a != e;
	r[31] = a != e;

	int ret = 0;
	int i;
	for (i = 0; i < 32; i++){
		ret <<= 1; ret |= (r[i] & 1);
	}
	printf("%08X\r\n", ret);

	FI fi;
	fi.f = a + b;	printf("%08X\r\n", fi.i);
	fi.f = a + c;	printf("%08X\r\n", fi.i);
	fi.f = a + d;	printf("%08X\r\n", fi.i);
	fi.f = a + e;	printf("%08X\r\n", fi.i);

	fi.f = a - b;	printf("%08X\r\n", fi.i);
	fi.f = a - c;	printf("%08X\r\n", fi.i);
	fi.f = a - d;	printf("%08X\r\n", fi.i);
	fi.f = a - e;	printf("%08X\r\n", fi.i);

	fi.f = a * b;	printf("%08X\r\n", fi.i);
	fi.f = a * c;	printf("%08X\r\n", fi.i);
	fi.f = a * d;	printf("%08X\r\n", fi.i);
	fi.f = a * e;	printf("%08X\r\n", fi.i);

	fi.f = a / b;	printf("%08X\r\n", fi.i);
	fi.f = a / a;	printf("%08X\r\n", fi.i);
	fi.f = a / d;	printf("%08X\r\n", fi.i);
	fi.f = a / e;	printf("%08X\r\n", fi.i);

	fi.f = e + b;	printf("%08X\r\n", fi.i);
	fi.f = e + c;	printf("%08X\r\n", fi.i);
	fi.f = e + d;	printf("%08X\r\n", fi.i);
	fi.f = e + e;	printf("%08X\r\n", fi.i);

	fi.f = e - b;	printf("%08X\r\n", fi.i);
	fi.f = e - c;	printf("%08X\r\n", fi.i);
	fi.f = e - d;	printf("%08X\r\n", fi.i);
	fi.f = e - e;	printf("%08X\r\n", fi.i);

	fi.f = e * b;	printf("%08X\r\n", fi.i);
	fi.f = e * c;	printf("%08X\r\n", fi.i);
	fi.f = e * d;	printf("%08X\r\n", fi.i);
	fi.f = e * e;	printf("%08X\r\n", fi.i);

	fi.f = e / b;	printf("%08X\r\n", fi.i);
	fi.f = e / a;	printf("%08X\r\n", fi.i);
	fi.f = e / d;	printf("%08X\r\n", fi.i);
	fi.f = e / e;	printf("%08X\r\n", fi.i);

	return 0;
}

  // int* pi = (int*)(REG_SRAM);
  // short* ps = (short*)(REG_SRAM);
  // char* pc = (char*)(REG_SRAM);

  // for(int i=64*2;i<512;i++){
  //   pi[i] = 0x12340000 + i;
  // }
  // pc[64*2*4 + 13] = 0xFF;

  //union UIF uif;
  //for(int j=0;j<1024;j++){
  //  //printf("%d \r\n", j);
  //  uif.vf = (float)j;
  //  printf("%d, %08X, %f\r\n", j, uif.vi, uif.vf);
  //}
  //printf("0\r\n");
  //volatile float a = 1.0f;
  //volatile float b = 2.0f;
  //volatile float c = a + b;
  //uif.vf = c;
  ////asm("nop");
  //printf("a\r\n");
  //printf("%08X\r\n", uif.vi);
  //printf("b\r\n");
  //while(1);
  // float test = 123.4;
  // printf("a:%f\r\n", test);
  // printf("K:%f\r\n", uif.vf);

//00005374 [086203e0] () custom 0, r2, r2, r3 # fadds r2, r2, r3
  //test();

  //DynamicMemInit();
  
  //int val = 21;
  //val = align(val, 4);
  //printf("%d", val);
  // ArrayList* arr = new ArrayList();
  // arr->init(0);
  

  // void* p1 = malloc(100);
  // printf("p1:%d\r\n", (int)p1);

  // void* p2 = malloc(100);
  // printf("p2:%d\r\n", (int)p2);

  // void* p3 = malloc(100);
  // printf("p3:%d\r\n", (int)p3);

  // free(p3);
  
  // void* p4 = malloc(50);
  // printf("p4:%d\r\n", (int)p4);

  // void* p5 = malloc(50);
  // printf("p5:%d\r\n", (int)p5);





void test(float a, float b){
  int c0 = a==b;
  int c1 = a> b;
  int c2 = a>=b;
  int c3 = a< b;
  int c4 = a<=b;
  int c5 = a!=b;
  c0 && printf("== ");
  c1 && printf(">  ");
  c2 && printf(">= ");
  c3 && printf("<  ");
  c4 && printf("<= ");
  c5 && printf("!= ");

  printf("\r\n");

}



int main(){
  IOWR(MYUART, 1 ,33);
  //systemInit();
  //initStdio();
  //memInit();
  //printf("fdsa:%d\r\n", getSystemVer());

  while(1);
  //                   0      1      2     3     4     5     6     7
  float value[] = {-3.3f, -2.2f, -1.1f, 0.0f, 0.0f, 1.1f, 2.2f, 3.3f};
  
  volatile float z;
  z = 0.0f;

  value[0] = value[0] / z;
  value[7] = value[7] / z;
  value[3] = -value[3];

  for(int i=0;i<8;i++){
    for(int j=0;j<8;j++){
      printf("%d, %d   ", i, j);
      test(value[i], value[j]);
    }
    printf("\r\n");
  }

  //char* src = "fjdsklafldsa";
  //char des[50];
  //for(int i=0;i<50;i++){
  //  des[i] = 0;
  //}
  //strcpy(des, src);
  //
  //
  //printf("%s", des);
  //
  //printf("%c", 'a');
  //printf("\r\n");
  //printf("fdsa{%f}", 123.1f);
  ////printf("\303\227");
  //
  //printf("%02X", 0xFA);//printByte: FA
  //printf("%02X", 0x0A);//printByte  0A
  //printf("%02X", 0x0A);//printByte  0A

  while(1);
  
}
